<?php
	
class Node_AjaxController extends Clu_Controller_Action_Hvn
{
	public function init()
	{
		$this->mService = new node_Model_NodeService();
		parent::init();
	}

	public function deleteNodeAction()
	{
		//need to have nodeTypeInfo first
		//to determine which node table ("nodes", "news"..) to work on
//		die($this->_request->getParam('type'));
		if (!$nodeType = $this->_request->getParam('type'))
		{
			$this->sendJsonError(null, "Node Type not specified");
            return;
		}

		//nodeType info for what to create in the form (and the 
		$nodeTypeInfo = $this->mService->getNodeTypeInfo($nodeType);
		if (!$nodeTypeInfo)
		{
			$this->sendJsonError(null, "Node Type " . $nodeType ." does not exist");
			return;
		}
		
		//new view the node
		$nodeId = $this->_request->getParam('id');
		$tmp = $this->mService->getNodeData($nodeTypeInfo, $nodeId);
		if (!$tmp['success'])
		{
			$this->sendJsonError(null, "Node content id ". $nodeId . " not found");
        	return;
		}
		else
			$node = $tmp['node'];
		//check permission if user is allowed to edit the node
		$sess = new Zend_Session_Namespace(CLU_NS);
		if (!isset($sess->userId))
		{
		 	$this->sendJsonError(null,t(726));
        	return;	
		}
		
		if (($sess->userId != $node['poster_id'] && strpos($sess->userPermissions, 'delete ' . $nodeType) === false) &&
				($sess->userId == $node['poster_id'] && strpos ($sess->userPermissions, 'delete own ' . $nodeType) === false))
		{
		 	$this->sendJsonError(null,t(726));
        	return;	
		}
			
		// Now delete the fucking node
		$ret = $this->mService->deleteNode($nodeTypeInfo, $nodeId) ;
		if ($this->_request->isXmlHttpRequest())
			$this->_helper->json->sendJson($ret);
		else
		{
			die('should not happen in AjaxControler');
		}
	}
	
	public function updatedStatusNodeAction()
	{
//		die_a($this->_request->getParams());
		//need to have nodeTypeInfo first
		//to determine which node table ("nodes", "news"..) to work on
		if (!$nodeType = $this->_request->getParam('type'))
		{
			$this->sendJsonError(null, "Node Type not specified");
            return;
		}
		
		//nodeType info for what to create in the form (and the 
		$nodeTypeInfo = $this->mService->getNodeTypeInfo($nodeType);
		if (!$nodeTypeInfo)
		{
			$this->sendJsonError(null, "Node Type " . $nodeType ." does not exist");
			return;
		}
		
		//new view the node
		$nodeId = $this->_request->getParam('nodeId');
		$tmp = $this->mService->getNodeData($nodeTypeInfo, $nodeId);
		if (!$tmp['success'])
		{
			$this->sendJsonError(null, "Node content id ". $nodeId . " not found");
        	return;
		}
		else
			$node = $tmp['node'];
		//check permission if user is allowed to edit the node
		$sess = new Zend_Session_Namespace(CLU_NS);
		if (!isset($sess->userId))
		{
		 	$this->sendJsonError(null,t(726));
        	return;	
		}
		
		if (($sess->userId != $node->poster_id && strpos($sess->userPermissions, 'update ' . $nodeType) === false) ||
				($sess->userId == $node->poster_id && strpos ($sess->userPermissions, 'update own ' . $nodeType) === false))
		{
		 	$this->sendJsonError(null,t(726));
        	return;	
		}
			
		// Now delete the fucking node
		$status = $this->_request->getParam('status');
		$ret = $this->mService->updateStatusNode($nodeTypeInfo, $nodeId, $status);
		if ($this->_request->isXmlHttpRequest())
			$this->_helper->json->sendJson($ret);
		else
		{
			die('should not happen in AjaxControler');
		}
	}
	
//	author : Dao Quang Duc
	public function setStatusAction()
	{
		$ids = $this->_request->getParam('ids');
		$ret['value'] = $this->_request->getParam('value');
		$ret['name'] = $this->_request->getParam('name');
		$ret['ids'] = explode(",", $ids);
		$nodeTypeInfo = $this->mService->getNodeTypeInfo('pm');
    	if($this->_request->isXmlHttpRequest())
		{
			if(count($ret['ids']) > 0)
			{
				if($ret['name'] == "status")
				{
					if($ret['value'] == 2)
					{
						$x = $this->mService->deleteNode($nodeTypeInfo, $ids);
						$ret['success'] = $x['success'];
					}
					else
					{
						$x = $this->mService->updateStatusNode($nodeTypeInfo, $ids, $ret['value']);
						$ret['success'] = $x['success'];
					}
				}
				else if($ret['name'] == "flag")
				{  
					$x = $this->mService->updateFlagNode($nodeTypeInfo, $ids, $ret['value']);
					$ret['success'] = $x['success'];
				}
			}
			$this->_helper->json->sendJson($ret);
		}
	}
	
	public function createNewUserCategoryAction()
	{
		$ret = array ("success" => true);
		if (!$nodeType = $this->_request->getParam('node-type'))
		{
			$this->sendJsonError(null, "Error. node type not specified");
            return;
		}
		$sess = new Zend_Session_Namespace(CLU_NS);
		if (!isset($sess->userId))
		{
		 	$this->sendJsonError(null,t(726));
        	return;	
		}
		$nodeTypeInfo = $this->mService->getNodeTypeInfo($nodeType);
		if (!$nodeTypeInfo['category_creatable'])
		{
			$this->sendJsonError(null, "Cannot create category for this kind of content");
            return;
		}
		//now try to create the category
		//require_once (APP_PATH. "/modules/category/Model/CategoryService.php");
		$categoryService = new category_Model_Category();
		$categoryName = trim($this->_request->getParam('category-name'));
		$ret = $categoryService->createNewUserCategory($sess->userId, $nodeType, $categoryName);
		if (!$ret['success'])
		{
			$this->_helper->json->sendJson($ret); return;
		}
		elseif (!$ret['created-new'])
		{
			$this->_helper->json->sendJson(array("success" => false, "errorCode" => "exists" , "errorMessage" => __("Category already exists"), "id" => $ret['id']));
		}
		else //success
			$this->_helper->json->sendJson(array("success" => true, "id" => $ret['id']));
	}
	
	public function updatedBlastAction()
    {
    	$ret = array ("success" => true);
    	$data = $this->_request->getParams();
    	
		if (!$nodeType = $data['type'])
		{
			$this->sendJsonError(null, "Error. node type not specified");
            return;
		}
		//die_a($this->_request->isXmlHttpRequest());
		if(($this->_request->isXmlHttpRequest()))
		{
			
			$sess = new Zend_Session_Namespace(CLU_NS);
			$nodeTypeInfo = $this->mService->getNodeTypeInfo($nodeType);
			$sql = "SELECT * FROM nodes WHERE type=blast AND poster_id = " .$data['poster_id'];
			$results = $this->mService->search($data, $nodeTypeInfo);
			$rets = $this->db->fetchRow($results);
			if($rets['poster_id'] != $data['poster_id'])
			{
				$this->mService->mNode->setNodeTableName($nodeTypeInfo['node_table_name']);
				$ret = $this->mService->insertNewNode($data, $nodeTypeInfo);
			}
			else{
				$node = $this->mService->findNodeByPrimary($nodeTypeInfo, $rets['id']);
				$ret = $this->mService->updateNode($data, $nodeTypeInfo, $node);
			}
			
			if($ret['success'])
			{
				$ret['result'] = $rets;
				$this->_helper->json->sendJson($ret);
			}
			else
			{
				$ret['success'] = false;
				$this->_helper->json->sendJson($ret);
			}
		}
    }
}
?>
